package com.wwz.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wwz.pojo.Device;
import com.wwz.pojo.query.DeviceQuery;
import com.wwz.service.IDeviceService;
import com.wwz.util.EcoBootException;
import com.wwz.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 设备控制层
 * @date 2022/03/26 15:11:31
 */
@RestController
@RequestMapping("/manage/device")
@Api(tags = "设备控制层")
public class DeviceController {

    @Resource
    private IDeviceService deviceService;

    @ApiOperation("设备列表")
    @GetMapping("/toPage")
    public Result toPage(DeviceQuery query){
        IPage<Device> deviceIPage = deviceService.toPage(query);
        return Result.ok(deviceIPage);
    }

    @ApiOperation("删除设备")
    @DeleteMapping("delete/{id}")
    public Result delete(@PathVariable String id){
        if (StrUtil.isEmpty(id)){
            throw new EcoBootException("id不能为空");
        }
        deviceService.removeById(id);
        return Result.ok("ok");
    }

    @ApiOperation("添加或修改设备信息")
    @PostMapping("/addOrUpdate")
    public Result addOrUpdate(@RequestBody Device device){

        if (StrUtil.isAllEmpty(device.getDeviceName())){
            throw new EcoBootException("信息为空");
        }
        deviceService.addOrUpdate(device);
        return Result.ok("ok");
    }
}
